﻿using Basic.Traceability.Domain.Freed;
using Basic.Traceability.ErrorCode;
using Basic.Traceability.Host.Api.Write.Application.Command.DeepProcessing.DeepProcessingAdditive;
using Basic.Traceability.Host.Api.Write.Application.Command.DeepProcessing.DeepProcessingAuxiliaryMaterials;
using Basic.Traceability.Host.Api.Write.Application.Command.DeepProcessing.DeepProcessingEnterpriseInformation;
using Basic.Traceability.Host.Api.Write.Application.Command.DeepProcessing.DeepProcessingPackagingMaterials;
using Basic.Traceability.Host.Api.Write.Application.Command.DeepProcessing.DeepProcessingProductionTeam;
using Basic.Traceability.Host.Api.Write.Application.Command.DeepProcessing.DeepProcessingRawMaterial;
using Basic.Traceability.Host.Api.Write.Application.Command.Freed.Breed56EnterpriseInformation;
using Basic.Traceability.Host.Api.Write.Application.Command.Freed.BreedingInformation;
using Basic.Traceability.Host.Api.Write.Application.Command.Freed.HealthInformation;
using Basic.Traceability.Host.Api.Write.Application.Command.Products.TraceableProducts;
using Basic.Traceability.Host.Api.Write.Application.Command.Slaughter.SlaughterEnterprise;
using Basic.Traceability.Host.Api.Write.Application.Command.Slaughter.SlaughterInformation;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Yitter.IdGenerator;

namespace Basic.Traceability.Host.Api.Write.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TracebilityController : ControllerBase
    {
        private readonly IMediator mediator;

        public TracebilityController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加商品信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateTraceableProducts(CreateTraceableProductsCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加养殖信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateBreedingInformation(CreateBreedingInformationCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// /添加养殖企业信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateBreed56EnterpriseInformation(CreateBreed56EnterpriseInformationCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加屠宰企业
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateSlaughterEnterprise(CreateSlaughterEnterpriseCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加免疫信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateHealthInformation(CreateHealthInformationCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加屠宰信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateSlaughterInformation(CreateSlaughterInformationCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// /删除商品信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<APIResult<int>> DeleteTraceableProducts(DeleteTraceableProductsCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 更新商品信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<APIResult<int>> UpdateTraceableProducts(UpdateTraceableProductsCommand request)
        {
            return mediator.Send(request);
        }


        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<string>> UploadImages(IFormFile file)
        {
            try
            {
                var ressult = new APIResult<string>();

                var str = YitIdHelper.NextId();

                string saveFile = Directory.GetCurrentDirectory() + @"/wwwroot/images/" + str + '_' + file.FileName;

                using (FileStream fs = new FileStream(saveFile, FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }

                var url = "http://" + Request.Host + "/images/" + str + '_' + file.FileName;
                ressult.Data = url;
                ressult.Code = APIEnums.Success;
                ressult.Msg = "上传成功";
                return ressult;
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加深加工添加级信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateDeepProcessingAdditive(CreateDeepProcessingAdditiveCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加深加工辅料信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateDeepProcessingAuxiliaryMaterials(CreateDeepProcessingAuxiliaryMaterialsCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加深加工企业信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateDeepProcessingEnterpriseInformation(CreateDeepProcessingEnterpriseInformationCommand request)
        {
            return mediator.Send(request);

        }
        /// <summary>
        /// 添加包装材料
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateDeepProcessingPackagingMaterials(CreateDeepProcessingPackagingMaterialsCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 创建生产团队
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateDeepProcessingProductionTeam(CreateDeepProcessingProductionTeamCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加深加工原料信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateDeepProcessingRawMaterial(CreateDeepProcessingRawMaterialCommand request)
        {
            return mediator.Send(request);
        }

    }
}
